//声明构造函数
function Promise(executor) {
    //状态属性
    this.PromiseState = 'pending';
    //结果值
    this.PromiseResult = undefined;

    //声明 success
    let success = (value) => {
        //修改状态
        this.PromiseState = 'fulfilled';
        //设置结果值
        this.PromiseResult = value;
    }

    //声明 fail
    let fail = (reason) => {
        //修改状态
        this.PromiseState = 'rejected';
        //设置结果值
        this.PromiseResult = reason;
    }

    try {
        //调用执行器函数
        executor(success, fail);
    } catch (e) {
        //修改状态和结果值
        fail(e);
    }
}

//原型声明 then 方法    on 当..时候      
Promise.prototype.then = function (onFulfilled, onRejected) {
    //执行回调
    //判断 promise 对象的状态
    if (this.PromiseState === 'fulfilled') {
        onFulfilled(this.PromiseResult);
    }

    if (this.PromiseState === 'rejected') {
        onRejected(this.PromiseResult);
    }
}